package com.cblz.bd.server.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cblz.bd.common.core.entity.BaAdminGroupAccess;
import com.cblz.bd.common.core.exception.BdAdminException;
import com.cblz.bd.redis.spring.boot.starter.service.RedisService;
import com.cblz.bd.server.core.annotation.Log;
import com.cblz.bd.server.core.mapper.BaAdminGroupAccessMapper;
import com.cblz.bd.server.core.service.BaAdminGroupAccessService;
import io.vavr.control.Option;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author yanjieping
 * @description 针对表【ba_admin_group_access(管理权限分组表)】的数据库操作Service实现
 * @createDate 2022-11-04 15:09:53
 */
@Slf4j
@RequiredArgsConstructor
@Service("baAdminGroupAccessService")
public class BaAdminGroupAccessServiceImpl extends ServiceImpl<BaAdminGroupAccessMapper, BaAdminGroupAccess>
        implements BaAdminGroupAccessService {

    private final RedisService redisService;

    /**
     * 根据后台管理员ID获取对应的用户组
     *
     * @param baAdminId
     * @return
     */
    @Override
    public int[] getGroupIdsByBaAdminId(Integer baAdminId) {
        return this.list(
                        new LambdaQueryWrapper<BaAdminGroupAccess>()
                                .eq(BaAdminGroupAccess::getUid, Option.of(baAdminId).getOrElseThrow(() -> new BdAdminException("获取对应的用户组失败，管理员ID参数为空"))))
                .stream().mapToInt(BaAdminGroupAccess::getGroupId).toArray();
    }

    @Override
    @Log(description = "新增角色组")
    public void add(Integer uid, List<Integer> group_arr) {
        group_arr.forEach(groupid -> {
            BaAdminGroupAccess baAdminGroupAccess = new BaAdminGroupAccess();
            baAdminGroupAccess.setUid(uid);
            baAdminGroupAccess.setGroupId(groupid);
            save(baAdminGroupAccess);
        });
    }

    @Log(description = "删除角色组")
    @Override
    public void delete(Integer uid) {
        LambdaQueryWrapper<BaAdminGroupAccess> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(BaAdminGroupAccess::getUid, uid);
        baseMapper.delete(lambdaQueryWrapper);
    }
}




